let Game = require('TxGame')
let {Global} = require('Global')
let Seat = require('Seat')

var {
    TexasPokerBetType,
    TexasPokerActionType,
    TexasPokerActions,
} = require("Texas")

var {RoomPlayerInfo} = require("CasinoProtocol")


cc.Class({
    extends: Seat,

    properties: {

        moneyLbl: cc.Label,
        rate: cc.Node,
        actor: cc.Node,//默认头像
        effects: cc.Node,
        place: cc.Node,

        rateMenu: cc.Node,//加注
        rateMenu2: cc.Node,//跟注
        rateMenu3: cc.Node,//ALLIN
        betMenu: cc.Node,//下底注
        betMenuLbl: cc.Label,//下底注Label

        basicMoney: cc.Node,
        basicMoneyLbl: cc.Label,
        betMoney: cc.Label,//下注总金额
        money: {
            set(Value) {
                if (value > 0) {
                    this.moneyLbl.node.active = true
                    this.moneyLbl.string = '' + value
                }
            }
        },

        user: {
            set(value) {
                this._user = value
                if (value) {
                    this.place.active = false
                    this.actor.active = true
                    let as = this.actor.getComponent('Actor')
                    this.cardStack.node.active = true
                    this.basicMoney.active = false
                    this.betMoney.node.active = true
                    this.betMoney.string = '' + Global.room.baseBetMoney
                } else {
                    this.actor.active = false
                    this.place.active = true
                }
            },
            get() {
                return this._user
            }
        },
        withBet: 0,//跟注
        bottomBet: 0,//第一次下注
        basicBet: 0,//主播设置底注
        allin: cc.Node,//玩家allin提示
        qipai: cc.Node,//玩家弃牌提示
        zhubo: cc.Node,//玩家是主播显示
        addPlayer: cc.Node,//点击添加
        maxBetG: cc.Node,//最大下注（跟注）
        maxBetA: cc.Node,//最大下注（AllIN）

        memberIdP: 0,
        // playerAction: {},



        // jiazhu:cc.Node,
        // action:{
        //     gameType: 1, 
        //     actionType: TexasPokerActionType.TexasPokerActionType_Bet,
        //     action: TexasPokerActions.TexasPokerAction_Bet,
        //     p1: 0,p2: 0,p3: 0,p4: 0
        // },//用户动作
    },

    showBaseMoneyIfNeed() {
        let baseMoney = Global.room.baseBetMoney
        if (baseMoney) {
            this.basicMoney.active = true
            this.basicMoneyLbl.string = '' + baseMoney
        } else {
            this.basicMoney.active = false
        }
    },

    // use this for initialization
    onLoad: function () {
        this.currentRate = 1
        this.effects = this.effects.getComponent('TxEffects')
        this.rateMenuComp = this.rateMenu.getComponent('TxBetMenu')
        // this.registerEvent();
    },


    registerEvent: function () {
        //RoomPlayerInfo:
        // uid: number;
        // userNick: string;
        // userImg: string;
        // isPresenter: number;
        // golds: number;
        // sessionId: number;
        // dbId: number;
        // srvId: number;
        // lineId: number;
        // platformId: number;
        // roomId: number;

        //info: RoomPlayerInfo;
        //index: number;
        window.sock.node.on("11-6", (e) => {//坐下进行游戏:Casino_SitDown
            if (window.user.isd) {
                this.zhubo.active = true;
            }
        })

        // errorNo: number;			// Int错误号
        //memberId: number;			// Int玩家座位序号
        //betType: number;			// Int下注类型
        //golds: number;				// Int本次下注金额
        //betCount: number;			// Int本局总下注金额
        //isPublic: number;			// Char是否公开，非0=公开
        window.sock.node.on("11-9", (e) => {//玩家下注：Casino_Bet
            // this.betMoney.active = true;
            // this.betMoney.string = e.detail.betCount.toString();
        })


        //  index: number;//Int
        //     state: number;//UInt
        // TexasPokerUserStatus_Pass = 5,
        // TexasPokerUserStatus_AllIn = 6,				//AllIn
        // TexasPokerUserStatus_Fold = 7,				// 玩家弃牌
        window.sock.node.on("11-11", (e) => {//玩家状态改变:Casino_ChangeUserState
            if (this.isMe()) {
                let state = e.detail.state;
                if (state === 6) {
                    this.allin.active = true;
                } else if (state === 7) {
                    this.qipai.active = true;
                }
            }
        })


        // cardList: number[]; //Char[MAX_ROOMPLAYER_CARD_COUNT]
        //     isPublic: number; //Char
        window.sock.node.on("11-16", (e) => {//改变玩家卡牌列表:Casino_ChangePlayerCardList
            if (this.isMe()) {
                let cards = e.detail.cardList
                this.cardStack.active = true;
                this.cardStack.isMe = true;
                for (let i = 0; i < cards.length; i++) {
                    let cardId = cards[i]
                    if (-1 !== cardId) {
                        this.cardStack.add(cardId);
                    } else {
                        break
                    }
                }
            }

        })


        // gameType: number;					//Int32游戏类型
        // index: number;						//Int32玩家座位号(0开始)
        // player: RoomPlayerInfo;

        //  uid: number;
        // userNick: string;
        // userImg: string;
        // isPresenter: number;
        // golds: number;
        // sessionId: number;
        // dbId: number;
        // srvId: number;
        // lineId: number;
        // platformId: number;
        // roomId: number;
        window.sock.node.on("11-14", (e) => {//改变玩家基础数据:Casino_ChangePlayerInfoVo
            //this.memberIdP = e.detail.index;
        })


        //  memberId: number;    //Int
        //     card: number;        //Char
        //     isPublic: number;    //Char
        window.sock.node.on("11-15", (e) => {//改变玩家某张牌数据:Casino_ChangePlayerCard
        })


        //  gameType: number;               //Int
        //     actionType: number;			    //Int 弹窗类型
        //     action: number;                  //UInt
        //     p1: number; //Int
        //     p2: number; //Int
        //     p3: number; //Int
        //     p4: number; //Int
        window.sock.node.on("11-17", (e) => {//通知客户端打开弹窗:Casino_OpenAction
            if (this.isMe()) {
                this.playerAction = e.detail;
                this.playerOperate();
            }
        })


        window.sock.node.on("11-19", (e) => {//通知客户端打开弹窗:Casino_OpenAction
            if (this.isMe()) {
                this.rateMenu.active = false;  //加注      
                this.rateMenu2.active = false;//跟注
                this.rateMenu3.active = false;//ALLIN
                this.betMenu.active = false;//下底注
            }
        })


        // gameType: number;	//Int 游戏类型
        //     actionType: number;	//Int 弹窗类型
        //     action: number;		//UInt 弹窗操作(A == (action & A)）
        //     p1: number;          //Int 备用参数
        //     p2: number;          //Int
        //     p3: number;          //Int
        //     p4: number;          //Int
        window.sock.node.on("11-23", (e) => {//改变弹窗指令:Casino_ChangeAction
            // this.playerAction = e.detail;
            // this.playerOperate();
        })

        // userCount: number; //Int
        //  resultList: IResultVo[]; //resultVo[MAX_ROOMUSER_COUNT];

        //         memberId: number;				//Int 玩家序号
        // uid: number;					//UInt64 玩家Uid
        // winGolds: number;				//Int64 本局赢得钻石数量
        // loseGolds: number;			    //Int64 本局输的钻石数量
        // sessionId: number;		        //UInt64 服务器用的临时用户ID(角色名)
        // dbId: number;				    //UShort 数据库分区Id
        // srvId: number;				    //UShort用户所在服务器Id
        // lineId: number;				    //UShort用户所在线服务器Id
        // platformId: number;			    //Int平台Id
        // roomId: number;				    //Int64房间ID
        // cardType: number;			    //Int 用户牌型
        // nowGolds: number;			    //UInt64 用户剩余钻石数量
        // isWin: number;				    //Char 用户是否是赢家
        window.sock.node.on("11-24", (e) => {//游戏结果:Casino_Result
            if (this.isMe()) {
                let resultList = e.detail.resultList;
                for (let i = 0; i < resultList.length; i++) {
                    let result = resultList[i];
                    play(result.cardType);
                }
            }
        })


    },

    /**
     * 玩家操作指令弹窗
     */
    playerOperate() {
        if (this.playerAction.actionType === TexasPokerActionType.TexasPokerActionType_Bet) {//下注
            this.betMenuLbl.string = '下底注' + window.room.minJoinBets.toString() + '金币';
            this.rateMenu.active = false;  //加注      
            this.rateMenu2.active = false;//跟注
            this.rateMenu3.active = false;//ALLIN
            this.betMenu.active = true;//下底注
        } else if (this.playerAction.actionType === TexasPokerActionType.TexasPokerActionType_Raise) {//加注
            this.rateMenu2.active = false;//跟注
            this.rateMenu3.active = false;//ALLIN
            this.betMenu.active = false;//下底注
            this.rateMenu.active = true;  //加注      
        } else if (this.playerAction.actionType === TexasPokerActionType.TexasPokerActionType_Raise) {//跟注
            if (this.playerAction.action === TexasPokerActions.TexasPokerAction_AllIn) {
                this.rateMenu3.active = true;//ALLIN
                this.betMenu.active = false;//下底注
                this.rateMenu.active = false;  //加注  
                this.rateMenu2.active = false;//跟注
            } else {
                this.rateMenu3.active = false;//ALLIN
                this.betMenu.active = false;//下底注
                this.rateMenu.active = false;  //加注  
                this.rateMenu2.active = true;//跟注
            }

        }
    },

    /**
     * 玩家坐下
     * 
     *   uid: number;              // 用户id unsigned long long 8
     *   userNick: string;         // 用户名 char 65 73
     *   userImg: string;             // char 257 330
     *   isPresenter: number;         // 不知道 char
     *   golds: number; 
     */
    sit() {
        window.sock.sit({ info: window.room, index: 1 });
    },

    /**
    * 下底注
    */
    bottomBetF() {
        window.sock.bet({ memberId: this.memberIdP, betType: TexasPokerBetType.TexasPokerBetType_Di, golds: window.room.minJoinBets });
    },

    /**
     * 弃牌
     */
    discardF() {
        window.sock.action({ actionType: this.playerAction.actionType, action: TexasPokerActions.TexasPokerAction_Fold });
    },

    /**
     * PASS
     */
    passF() {
        window.sock.action({ actionType: this.playerAction.actionType, action: TexasPokerActions.TexasPokerAction_Pass });
    },

    /**
     * 跟注
     */
    withBetF() {
        window.sock.bet({ memberId: memberIdP, betType: TexasPokerBetType.TexasPokerBetType_Di, golds: this.playerAction.p2 });
    },

    /**
     * ALLIN
     */
    allInF() {
        window.sock.action({ actionType: this.playerAction.actionType, action: TexasPokerActions.TexasPokerAction_AllIn });
    },

    /**
     * 加注
     */
    fillingF() {
        this.rateMenu.active = false;
        // cc.find("Canvas/ui/yonghushezhi").node.active = true;
        window.sock.node.emit("jiazhu")
    },

    /**
     * 隐藏加号，显示头像
     */
    displayActor() {
        this.actor.active = true;
        this.addPlayer.active = false;
    },

    /**
     * 是否显示牌
     */
    displayCard(data) {
        if (data.type == 1) {//不显示牌
            this.cardStack.active = false;
            this.cardStack.clear();
        } else if (data.type == 2) {//发牌：自己显示明牌，其他暗牌
            if (data.isMe = true) {
                this.cardStack.active = true;
                this.cardStack.isMe = true;
            } else {
                this.cardStack.active = true;
                this.cardStack.isMe = false;
            }
        } else if (data.type == 3) {//开牌
            this.cardStack.active = true;
            this.cardStack.isMe = true;
        }
    },

    /**
     * 是否是自己
     */
    isMe() {
        if (this.playerInfo.uid === window.user.uid) {
            return true;
        } else {
            return false;
        }
    },

    kickOrStand() {
        Global.gameInstance.kickOrStand(this.user.id)
    },

    redouble(x) {
        this.betMoney.node.active = true
        this.betMoney.string = '' + Global.room.baseBetMoney * x
        this.currentRate = x
    },

    callCard(card) {
        this.cardStack.isMe = Global.user._id === this.user.id
        this.cardStack.add(card)
    },

    clear() {
        this.currentRate = 1
        this.betMoney.node.active = true
        this.betMoney.string = '' + Global.room.baseBetMoney
        this.cardStack.clear()
        this.effects.clear()
    },

    flipCards() {
        this.cardStack.flip()
    },

    showReBetmenu() {
        if (this.cards.length) {
            this.countDown.startCount()
            this.countDown.complete = () => { this.rate1() }
            if (this.cards.length > 0) {
                this.rateMenu.active = true
                this.rateMenuComp.alreadyBetRate = this.currentRate
            }
        }
    },

    hidenReBetmenu() {
        this.countDown.stopCount()
        this.rateMenu.active = false
    },

    /**
     * 播放指定动画
     * 
     * @param {any} k
     */
    play(k) {
        this.effects.play(k)
    }
});
